package com.training1.core.web.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.entity.BatchWithDrawalEntity;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.service.BatchWithDrawalService;
import com.training1.core.modules.service.MemberService;
import com.training1.util.SequenceNumberUtil;
@Controller
@RequestMapping("balance")
public class BalanceController extends BaseController {
	
	@Resource
	private MemberService memService;
	@Resource
	private BatchWithDrawalService bwdService;
	
	@RequestMapping({"/",""})
	public String balance(){
		return "yixiang/balanceSetting";
	}
	
	@RequestMapping("getMember")
	@ResponseBody
	public MemberEntity getMember(String cellphone){
		MemberEntity mem = memService.findMember(cellphone);
		return mem;
	}
	
	@RequestMapping(value="saveOne", method={RequestMethod.GET})
	@ResponseBody
	public RespJson save(String cellPhone,String memberName,String bankCardId,String bankName,String avaibleCash,String withdrawCash){
		BatchWithDrawalEntity bde = new BatchWithDrawalEntity();
		bde.setOrderNumber(SequenceNumberUtil.generateOrderNumber1());
		bde.setPhone(cellPhone);
		bde.setMoney(new Double(withdrawCash));
		bde.setName(memberName);
		bde.setCardNumber(bankCardId);
		bde.setBankforCard(bankName);
		bde.setStatus(1);
		bde.setPayStatus(0);
		
		MemberEntity mem = memService.findMember(cellPhone);
		mem.setAvaibleCash(new BigDecimal(mem.getAvaibleCash().doubleValue()-new Double(withdrawCash)));
		mem.setUpdateBy(bde.getUpdateBy());
		boolean flag = bwdService.saveOne(bde,mem)>0;
		if(!flag){
			return RespJsonFactory.buildFailure();
		}
		return RespJsonFactory.buildSuccess();
	}
}
